home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / libsrc / gmon.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  9KB  |  332 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #include <unistd.h>
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/fcntl.h>
  40.  
  41. #include "gmon.h"
  42.  
  43. extern void mcount(void) asm ("mcount");
  44. extern void start_text () asm ("exec");
  45. /* extern char *minbrk asm ("minbrk"); */
  46.  
  47. void moncontrol(int mode);
  48.  
  49.     /*
  50.      *    froms is actually a bunch of unsigned shorts indexing tos
  51.      */
  52. static int        profiling = 3;
  53. static unsigned short    *froms;
  54. static struct tostruct    *tos = 0;
  55. static long        tolimit = 0;
  56. static char        *s_lowpc = 0;
  57. static char        *s_highpc = 0;
  58. static unsigned long    s_textsize = 0;
  59.  
  60. static int    ssiz;
  61. static char    *sbuf;
  62. static int    s_scale;
  63.     /* see profil(2) where this is describe (incorrectly) */
  64. #define        SCALE_1_TO_1    0x10000L
  65.  
  66. #define    MSG "No space for profiling buffer(s)\n"
  67.  
  68. static inline void *
  69. alloc(int s)
  70. {
  71.   void *res = malloc (s);
  72.   if (res)
  73.     bzero (res, s);
  74.   return res;
  75. }
  76.  
  77. void monstartup(char *lowpc, char *highpc)
  78. {
  79.     int            monsize;
  80.     char        *buffer;
  81.     register int    o;
  82.  
  83.     /*
  84.      *    round lowpc and highpc to multiples of the density we're using
  85.      *    so the rest of the scaling (here and in gprof) stays in ints.
  86.      */
  87.     lowpc = (char *)
  88.         ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
  89.     s_lowpc = lowpc;
  90.     highpc = (char *)
  91.         ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
  92.     s_highpc = highpc;
  93.     s_textsize = highpc - lowpc;
  94.     monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
  95.     buffer = alloc( monsize );
  96.     if ( buffer == 0 ) {
  97.     write( 2 , MSG , sizeof(MSG) );
  98.     return;
  99.     }
  100.     froms = (unsigned short *) alloc ( s_textsize / HASHFRACTION );
  101.     if ( froms == 0 ) {
  102.     write( 2 , MSG , sizeof(MSG) );
  103.     froms = 0;
  104.     return;
  105.     }
  106.     tolimit = s_textsize * ARCDENSITY / 100;
  107.     if ( tolimit < MINARCS ) {
  108.     tolimit = MINARCS;
  109.     } else if ( tolimit > 65534 ) {
  110.     tolimit = 65534;
  111.     }
  112.     tos = (struct tostruct *) alloc( tolimit * sizeof( struct tostruct ) );
  113.     if ( tos == 0 ) {
  114.     write( 2 , MSG , sizeof(MSG) );
  115.     froms = 0;
  116.     tos = 0;
  117.     return;
  118.     }
  119. /*    minbrk = sbrk(0); */
  120.     tos[0].link = 0;
  121.     sbuf = buffer;
  122.     ssiz = monsize;
  123.     ( (struct phdr *) buffer ) -> lpc = (void *)(lowpc - ((char *)start_text - 4));
  124.     ( (struct phdr *) buffer ) -> hpc = (void *)(highpc - ((char *)start_text - 4));
  125.     ( (struct phdr *) buffer ) -> ncnt = ssiz;
  126.     monsize -= sizeof(struct phdr);
  127.     if ( monsize <= 0 )
  128.     return;
  129.     o = highpc - lowpc;
  130.     if( monsize < o )
  131.     {
  132.     int quot = o / monsize;
  133.  
  134.     if (quot >= 0x10000)
  135.         s_scale = 1;
  136.     else if (quot >= 0x100)
  137.         s_scale = 0x10000 / quot;
  138.     else if (o >= 0x800000)
  139.         s_scale = 0x1000000 / (o / (monsize >> 8));
  140.     else
  141.         s_scale = 0x1000000 / ((o << 8) / monsize);
  142.     }
  143.     else
  144.     s_scale = SCALE_1_TO_1;
  145.     moncontrol(1);
  146. }
  147.  
  148. void _mcleanup(void)
  149. {
  150.     int            fd;
  151.     int            fromindex;
  152.     int            endfrom;
  153.     char        *frompc;
  154.     int            toindex;
  155.     struct rawarc    rawarc;
  156.  
  157.     moncontrol(0);
  158.     fd = creat( "gmon.out" , 0666 );
  159.     if ( fd < 0 ) {
  160.     perror( "mcount: gmon.out" );
  161.     return;
  162.     }
  163. #   ifdef DEBUG
  164.     fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
  165. #   endif DEBUG
  166.     write( fd , sbuf , ssiz );
  167.     endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
  168.     for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
  169.     if ( froms[fromindex] == 0 ) {
  170.         continue;
  171.     }
  172.     frompc = (void *)(s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms)) - ((char *)start_text - 4));
  173.     for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
  174. #        ifdef DEBUG
  175.         fprintf( stderr ,
  176.             "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
  177.             frompc , tos[toindex].selfpc , tos[toindex].count );
  178. #        endif DEBUG
  179.         rawarc.raw_frompc = (unsigned long) frompc;
  180.         rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
  181.         rawarc.raw_count = tos[toindex].count;
  182.         write( fd , &rawarc , sizeof rawarc );
  183.     }
  184.     }
  185.     close( fd );
  186. }
  187.  
  188. void mcount(void)
  189. {
  190.     register char            *selfpc;
  191.     register unsigned short        *frompcindex;
  192.     register struct tostruct    *top;
  193.     register struct tostruct    *prevtop;
  194.     register long            toindex;
  195.  
  196.     /*
  197.      *    find the return address for mcount,
  198.      *    and the return address for mcount's caller.
  199.      */
  200.  
  201.     /* selfpc = pc pushed by mcount call.
  202.        This identifies the function that was just entered.  */
  203.     selfpc = (char *) __builtin_return_address (0) - (int)((char *)start_text - 4);
  204.     /* frompcindex = pc in preceding frame.
  205.        This identifies the caller of the function just entered.  */
  206.     frompcindex = (void *) __builtin_return_address (1);
  207.     /*
  208.      *    check that we are profiling
  209.      *    and that we aren't recursively invoked.
  210.      */
  211.     if (profiling) {
  212.         goto out;
  213.     }
  214.     profiling++;
  215.     /*
  216.      *    check that frompcindex is a reasonable pc value.
  217.      *    for example:    signal catchers get called from the stack,
  218.      *            not from text space.  too bad.
  219.      */
  220. #ifdef DEBUG
  221.         fprintf (stderr, "from $%lx, self $%lx (low = $%lx)\n",
  222.              frompcindex, selfpc, s_lowpc);
  223. #endif
  224.     frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
  225.     if ((unsigned long)frompcindex > s_textsize) {
  226.  
  227.         goto done;
  228.     }
  229.     frompcindex =
  230.         &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
  231.     toindex = *frompcindex;
  232.     if (toindex == 0) {
  233.         /*
  234.          *    first time traversing this arc
  235.          */
  236.         toindex = ++tos[0].link;
  237.         if (toindex >= tolimit) {
  238.             goto overflow;
  239.         }
  240.         *frompcindex = toindex;
  241.         top = &tos[toindex];
  242.         top->selfpc = selfpc;
  243.         top->count = 1;
  244.         top->link = 0;
  245.         goto done;
  246.     }
  247.     top = &tos[toindex];
  248.     if (top->selfpc == selfpc) {
  249.         /*
  250.          *    arc at front of chain; usual case.
  251.          */
  252.         top->count++;
  253.         goto done;
  254.     }
  255.     /*
  256.      *    have to go looking down chain for it.
  257.      *    top points to what we are looking at,
  258.      *    prevtop points to previous top.
  259.      *    we know it is not at the head of the chain.
  260.      */
  261.     for (; /* goto done */; ) {
  262.         if (top->link == 0) {
  263.             /*
  264.              *    top is end of the chain and none of the chain
  265.              *    had top->selfpc == selfpc.
  266.              *    so we allocate a new tostruct
  267.              *    and link it to the head of the chain.
  268.              */
  269.             toindex = ++tos[0].link;
  270.             if (toindex >= tolimit) {
  271.                 goto overflow;
  272.             }
  273.             top = &tos[toindex];
  274.             top->selfpc = selfpc;
  275.             top->count = 1;
  276.             top->link = *frompcindex;
  277.             *frompcindex = toindex;
  278.             goto done;
  279.         }
  280.         /*
  281.          *    otherwise, check the next arc on the chain.
  282.          */
  283.         prevtop = top;
  284.         top = &tos[top->link];
  285.         if (top->selfpc == selfpc) {
  286.             /*
  287.              *    there it is.
  288.              *    increment its count
  289.              *    move it to the head of the chain.
  290.              */
  291.             top->count++;
  292.             toindex = prevtop->link;
  293.             prevtop->link = top->link;
  294.             top->link = *frompcindex;
  295.             *frompcindex = toindex;
  296.             goto done;
  297.         }
  298.  
  299.     }
  300. done:
  301.     profiling--;
  302.     /* and fall through */
  303. out:
  304.     return;        /* normal return restores saved registers */
  305.  
  306. overflow:
  307.     profiling++; /* halt further profiling */
  308. #   define    TOLIMIT    "mcount: tos overflow\n"
  309.     write(2, TOLIMIT, sizeof(TOLIMIT));
  310.     goto out;
  311. }
  312.  
  313. /*
  314.  * Control profiling
  315.  *    profiling is what mcount checks to see if
  316.  *    all the data structures are ready.
  317.  */
  318. void moncontrol(int mode)
  319. {
  320.     if (mode) {
  321.     /* start */
  322.     profil(sbuf + sizeof(struct phdr), ssiz - sizeof(struct phdr),
  323.         (int)s_lowpc, s_scale);
  324.     profiling = 0;
  325.     } else {
  326.     /* stop */
  327.     profil((char *)0, 0, 0, 0);
  328.     profiling = 3;
  329.     }
  330. }
  331.  
  332.